<?php
namespace app\index\controller;

use app\index\model\AcckuanModel;
use app\index\model\AccountModel;
use app\index\model\BondJiaoModel;
use app\index\model\BondShouModel;
use app\index\model\BondTuiModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\OptionFeiModel;
use app\index\model\ShouHuanModel;
use app\index\model\ShouQiListModel;
use app\index\model\ShouQiModel;
use app\index\model\ShouTouListModel;
use app\index\model\ShouTouModel;
use app\index\model\ShouXiangKouModel;
use app\index\model\ShouXiangListModel;
use app\index\model\ShouXiangModel;
use app\index\model\UserModel;
use app\index\model\ZhiJieModel;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Shou extends Common
{
    public function detail_shou_tou(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listexplain']) && !empty($param['listexplain'])){
            $where[] = ['list.explain','like', '%'.$param['listexplain'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }

        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['list.feitype','=', $param['feitype']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_tbf']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';

        $data['data'] = ShouTouListModel::alias('list')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bill.sericnum,
            list.feitype,list.money as listmoney,list.explain as listexplain')
            ->join('shou_tou shou','shou.id=list.shouid')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_tou', 'shou'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_shou_tou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=','0'];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = ShouTouModel::alias('shou')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bill.sericnum,bill.modename')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_tou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('shou_tou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_shou_tou(Request $request){
        $param = $request->post();
        $data = ShouTouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ShouTouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'shou_tou', $data['id'], '投标费收取');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_shou_tou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ShouTouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $list = ShouTouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('shou_tou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_kou_shou_xiang(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['feitype_name']) && !empty($param['feitype_name'])){
            $where[] = ['fei.name','like', '%'.$param['feitype_name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listexplain']) && !empty($param['listexplain'])){
            $where[] = ['list.explain','like', '%'.$param['listexplain'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['data'] = ShouXiangKouModel::alias('list')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bill.sericnum,
            list.id as listid,list.feitype,list.money as listmoney,list.explain as listexplain,fei.name as feitype_name')
            ->join('shou_xiang shou','shou.id=list.shouid')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('option_fei fei','fei.id=list.feitype')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_xiang', 'shou'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = '联营费用(扣收)';
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_shou_xiang(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['listexplain']) && !empty($param['listexplain'])){
            $where[] = ['list.explain','like', '%'.$param['listexplain'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shou.baner','like', '%'.$param['baner'].'%'];
        }




        if(isset($param['feitype_name']) && !empty($param['feitype_name'])){
            $where[] = ['fei.name','like', '%'.$param['feitype_name'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = ShouXiangListModel::alias('list')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bill.sericnum,
            list.feitype,list.money as listmoney,list.explain as listexplain,fei.name as feitype_name')
            ->join('shou_xiang shou','shou.id=list.shouid')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('option_fei fei','fei.id=list.feitype')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_xiang', 'shou'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_shou_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=','0'];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['feilei_option']) && !empty($param['feilei_option'])){
            $param['feilei'] = (new UserService())->get_optionfeiid_bynum($param['feilei_option']);
        }




        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['feilei']) && !empty($param['feilei'])){
            $where[] = ['shou.feilei','=', $param['feilei']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['feilei'] = (new UserService())->get_option_fei_slet(['xmsr_gchk','xmsr_gsdz','xmsr_lyfbyj']);
        $data['is_feilei'] = isset($param['feilei']) ? $param['feilei'] : '';

        $data['data'] = ShouXiangModel::alias('shou')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bill.sericnum,bill.modename')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_xiang', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('shou_xiang');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_shou_xiang(Request $request){
        $param = $request->post();
        $data = ShouXiangModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ShouXiangModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'shou_xiang', $data['id'], '项目收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_shou_xiang(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ShouXiangModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $list = ShouXiangListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
        }
        $data['list'] = $list;

        $kou = ShouXiangKouModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($kou as &$item){
            $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            $item['lai'] = $item['daikouqiid'] > 0 ? '待扣' : '/';
        }
        $data['kou'] = $kou;

        $data['files'] = (new OptService())->get_file('shou_xiang', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_shou_huan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=','0'];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['jieid']) && !empty($param['jieid'])){
            $where[] = ['shou.jieid','=', $param['jieid']];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['shou.created_user','=', $param['created_user']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shou.duier','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = ShouHuanModel::alias('shou')
            ->field('shou.*,acc.name as accname,acc.accnum,bill.sericnum,bill.modename')
            ->leftJoin('zhi_jie jie','jie.id=shou.jieid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_huan', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('shou_huan');

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_shou_huan(Request $request){
        $param = $request->post();
        $data = ShouHuanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ShouHuanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'shou_huan', $data['id'], '备用金还款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_shou_huan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ShouHuanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
        $data['data']['feitype_name'] = OptionFeiModel::where(['id'=>$data['data']['feitype']])->value('name');
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['jieidinfo'] = ZhiJieModel::where(['id'=>$data['data']['jieid']])->find();

        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $data['files'] = (new OptService())->get_file('shou_huan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_shou_qi(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shou.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['list.feitype','=', $param['feitype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['feitype'] = (new UserService())->get_option_fei_slet(['qtsr_qtsr']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';

        $data['data'] = ShouQiListModel::alias('list')
            ->field('shou.*,acc.name as accname,acc.accnum,bill.sericnum,
            list.feitype,list.money as listmoney,list.explain as listexplain')
            ->join('shou_qi shou','shou.id=list.shouid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_qi', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_shou_qi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=','0'];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shou.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = ShouQiModel::alias('shou')
            ->field('shou.*,acc.name as accname,acc.accnum,bill.sericnum,bill.modename')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('shou_qi', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('shou_qi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_shou_qi(Request $request){
        $param = $request->post();
        $data = ShouQiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ShouQiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'shou_qi', $data['id'], '其他收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_shou_qi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ShouQiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $list = ShouQiListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('shou_qi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_bond_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=','0'];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['shou.feitype','=', $param['feitype']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = BondShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bill.sericnum,bill.modename')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bond_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bond_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_bond_shou(Request $request){
        $param = $request->post();
        $data = BondShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            BondShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bond_shou', $data['id'], '保证金收取');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_bond_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BondShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feitype_name'] = OptionFeiModel::where(['id'=>$data['data']['feitype']])->value('name');
        $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;


        $data['files'] = (new OptService())->get_file('bond_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_bond_tui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tui.deleted_user','=','0'];
        $where[] = ['tui.comid','=',$this->userinfo['comid']];
        $where[] = ['tui.isturn','=',1];
        $where[] = ['tui.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['tui.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['tui.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['tui.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['jiao.feitype','=', $param['feitype']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['tui.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['tui.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['tui.status','=', $param['status']];
        }


        $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = BondTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname,acc.name as accname,acc.accnum,jiao.feitype,bill.sericnum,bill.modename')
            ->join('engin e','e.id=tui.enginid')
            ->leftJoin('account acc','acc.id=tui.accid')
            ->leftJoin('bond_jiao jiao','jiao.id=tui.jiaoid')
            ->leftJoin('flow_bill bill','bill.id=tui.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bond_tui', 'tui'))
            ->order('tui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bond_tui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_bond_tui(Request $request){
        $param = $request->post();
        $data = BondTuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            BondTuiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bond_tui', $data['id'], '保证金退回');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_bond_tui(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BondTuiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feitype_name'] = OptionFeiModel::where(['id'=>$data['data']['feitype']])->value('name');
        $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['jiaoidinfo'] = BondJiaoModel::where(['id'=>$data['data']['jiaoid']])->find();

        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $data['files'] = (new OptService())->get_file('bond_tui', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




}
